<!--
Copyright 2014 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!DOCTYPE html><meta charset="UTF-8">
<style>
#target {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 100px;
  height: 100px;
  background-color: green;
}
</style>
<div id="target"></div>
<script src="../bootstrap.js"></script>
<script>
'use strict';

function createPlayer() {
  return target.animate({ left: '100px' }, {
    iterations: 2,
    duration: 0.25,
  });
}

var player;

player = createPlayer();
var singleEventHandler = false;
player.addEventListener('finish', function() { singleEventHandler = true; })
var testA = async_test('Check finish event with single handler');
player.addEventListener('finish', function() {
  testA.step(function() { assert_true(singleEventHandler); });
  testA.done();
});

player = createPlayer();
var eventHandlingCount = 0;
var addMultipleTimes = function() { eventHandlingCount++; };
player.addEventListener('finish', addMultipleTimes);
player.addEventListener('finish', addMultipleTimes);
var testB = async_test('Check finish event with single handler added multiple times');
player.addEventListener('finish', function() {
  testB.step(function() { assert_equals(eventHandlingCount, 1); });
  testB.done();
});

player = createPlayer();
var multiEventHandlerA = false;
var multiEventHandlerB = false;
var multiEventHandlerC = false;
player.addEventListener('finish', function() { multiEventHandlerA = true; })
player.addEventListener('finish', function() { multiEventHandlerB = true; })
player.addEventListener('finish', function() { multiEventHandlerC = true; })
var testC = async_test('Check finish event with multiple handlers');
player.addEventListener('finish', function() {
  testC.step(function() { assert_true(multiEventHandlerA && multiEventHandlerB && multiEventHandlerC); });
  testC.done();
});

player = createPlayer();
var retainEventHandler = false;
player.addEventListener('finish', function() { retainEventHandler = true; });
var removeEventHandler = true;
var eventHandlerToRemove = function() { removeEventHandler = false; }
player.addEventListener('finish', eventHandlerToRemove);
player.removeEventListener('finish', eventHandlerToRemove);
var testD = async_test('Check finish event with event handler removed');
player.addEventListener('finish', function() {
  testD.step(function() { assert_true(retainEventHandler && removeEventHandler); });
  testD.done();
});

player = createPlayer();
var onEventHandler = false;
player.onfinish = function() { onEventHandler = true; };
var testE = async_test('Check onfinish handler');
player.addEventListener('finish', function() {
  testE.step(function() { assert_true(onEventHandler); });
  testE.done();
});

player = createPlayer();
var clearOnEventHandler = true;
player.onfinish = function() { clearOnEventHandler = false; };
player.onfinish = null;
var testF = async_test('Check clearing onfinish handler');
player.addEventListener('finish', function() {
  testF.step(function() { assert_true(clearOnEventHandler); });
  testF.done();
});

player = createPlayer();
var onEventOrdering = '';
player.addEventListener('finish', function() { onEventOrdering += 'a'; });
player.onfinish = function() { onEventOrdering += '!'; };
player.addEventListener('finish', function() { onEventOrdering += 'b'; });
player.onfinish = function() { onEventOrdering += 'c'; };
player.addEventListener('finish', function() { onEventOrdering += 'd'; });
var testG = async_test('Check onfinish handler ordering and overriding');
player.addEventListener('finish', function() {
  testG.step(function() { assert_equals(onEventOrdering, 'abcd'); });
  testG.done();
});

player = createPlayer();
var onEventOrderingAfterRemoval = '';
var eventHandlerToRemoveA = function() { onEventOrderingAfterRemoval += '!'; };
var eventHandlerToRemoveB = function() { onEventOrderingAfterRemoval += '!!'; };
player.addEventListener('finish', function() { onEventOrderingAfterRemoval += 'a'; });
player.addEventListener('finish', eventHandlerToRemoveA);
player.onfinish = function() { onEventOrderingAfterRemoval += 'b'; };
player.addEventListener('finish', function() { onEventOrderingAfterRemoval += 'c'; });
player.addEventListener('finish', eventHandlerToRemoveB);
player.removeEventListener('finish', eventHandlerToRemoveA);
player.removeEventListener('finish', eventHandlerToRemoveB);
var testH = async_test('Check onfinish handler ordering after removal');
player.addEventListener('finish', function() {
  testH.step(function() { assert_equals(onEventOrderingAfterRemoval, 'abc'); });
  testH.done();
});

player = createPlayer();
var onEventDuplication = 0;
var duplicateFunctionA = function() { onEventDuplication += 1; };
player.addEventListener('finish', duplicateFunctionA);
player.onfinish = duplicateFunctionA;
var testI = async_test('Check onfinish handler duplication');
player.addEventListener('finish', function() {
  testI.step(function() { assert_equals(onEventDuplication, 2); });
  testI.done();
});

player = createPlayer();
var eventDuplication = 0;
var duplicateFunctionB = function() { eventDuplication += 1; };
player.addEventListener('finish', duplicateFunctionB);
player.addEventListener('finish', duplicateFunctionB);
var testJ = async_test('Check finish event handler duplication');
player.addEventListener('finish', function() {
  testJ.step(function() { assert_equals(eventDuplication, 1); });
  testJ.done();
});

timing_test(function() { at(1, function(){}); }, 'Force testharness to execute to 1 second.');

</script>
